package practise.hibernate.persistence;

import org.hibernate.annotations.*;
import practise.hibernate.interceptor.LogInterceptor;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Entity(name = "person")
@org.hibernate.annotations.Entity(selectBeforeUpdate = false, optimisticLock = OptimisticLockType.ALL , dynamicUpdate = true)
@Table(name = "PERSON")
@EntityListeners(LogInterceptor.class)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER,name = "level")
@DiscriminatorValue("0")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;

    @Embedded
    private PersonName personName = new PersonName();

    @Column(name = "age")
    private int age;
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ADDRESS_ID")
    private Address address;
    @ManyToMany(mappedBy = "personList",fetch = FetchType.LAZY)
    private List<MoiveRecord> moiveRecords = new ArrayList<MoiveRecord>();

    public List<MoiveSite> getMoiveSites() {
        return moiveSites;
    }

    public void setMoiveSites(List<MoiveSite> moiveSites) {
        this.moiveSites = moiveSites;
    }

    @OneToMany(mappedBy = "person", cascade = {CascadeType.PERSIST , CascadeType.MERGE})
    private List<MoiveSite> moiveSites = new ArrayList<MoiveSite>();

    @Version
    @Column( name = "version")
    private Integer version;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }



    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getFirstName() {
        return personName.getFirstName();
    }

    public void setFirstName(String firstName) {
        personName.setFirstName(firstName);
    }

    public String getLastName() {
        return personName.getLastName();
    }

    public void setLastName(String lastName) {
        personName.setLastName(lastName);
    }


    public Integer getVersion() {
       return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public List<MoiveRecord> getMoiveRecords() {
        return moiveRecords;
    }

    public void setMoiveRecords(List<MoiveRecord> moiveRecords) {
        this.moiveRecords = moiveRecords;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;

        Person person = (Person) o;

        if (age != person.age) return false;
        if (personName != null ? !personName.equals(person.personName) : person.personName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = personName != null ? personName.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }
}
